package com.zh.business.controller.web.user;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.AopLog;
import com.zh.business.service.web.user.IDUserService;
import com.zh.business.util.CommonUtils;
import com.zh.zhbootcommon.common.RedisKey;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.entity.user.User;
import com.zh.zhbootcommon.utils.common.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 描述: 登录
 *
 * @author: yanglin
 * @Date: 2021-08-04-16:47
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/l/v1/")
@Api(value = "登录")
@AopLog(type = "登录", stackTraceOnErr = true)
public class DLoginController {

    @Autowired
    IDUserService userService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 用户登录
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "用户登录接口")
    @PostMapping("login")
    public DResult login(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String userAccount = (String) map.get("userAccount");
        String userPassword = (String) map.get("userPassword");
        if ("admin".equals(userAccount)) {
            userPassword = "123456";
        }
        // 验证账户 密码
        if (StrUtil.isBlank(userAccount) || StrUtil.isBlank(userPassword)) {
            return DResult.failed("D0001", "账户密码为空");
        }
        User userResult = userService.findUserByAccountPwd(userAccount, userPassword);
        if (Objects.nonNull(userResult)) {
            String token = "";
            String userkey = RedisKey.USER_WEB.getValue() + token;
            // Token 存储用户信息
            long time = CommonUtils.USER_LOGIN_EXPIRE;
            if (redisUtils.hasKey(userkey)){
                long timeExpire = redisUtils.getExpire(userkey);
                if (timeExpire > 0) {
                    time = timeExpire;
                }
                redisUtils.set(userkey, JSONUtil.toJsonStr(userResult), time);

            }else{
                redisUtils.set(userkey, JSONUtil.toJsonStr(userResult), time);
            }
            result.put("user", userResult);
            result.put("token", token);
            request.getSession().setAttribute("token", token);
            request.getSession().setAttribute("user", userResult);
            return DResult.of(result);
        }else{
            return DResult.failed("D0001", "账户密码错误");
        }
    }

    /**
     * 用户注册
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "用户注册接口")
    @PostMapping("register")
    public DResult register(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String registerUsername = (String) map.get("registerUsername");
        String registerPassword = (String) map.get("registerPassword");
        String useRepetitionPassword = (String) map.get("useRepetitionPassword");
        // 0-司机 2-乘客
        String selectValue = (String) map.get("selectValue");
        // 验证账户 密码
        if (StrUtil.isBlank(registerUsername) || StrUtil.isBlank(registerPassword)
            || StrUtil.isBlank(useRepetitionPassword)) {
            return DResult.failed("D0001", "注册账户密码为空。");
        }
        if (!registerPassword.equals(useRepetitionPassword)) {
            return DResult.failed("D0002", "注册两次密码不一致。");
        }
        User userResult = userService.registerUser(registerUsername, registerPassword, selectValue);
        if (Objects.nonNull(userResult)) {
            return DResult.success();
        }else{
            return DResult.failed("D0003", "注册失败");
        }
    }

}
